//
//  WeiboMainViewController.swift
//  微博
//
//  Created by LH on 2017/5/23.
//  Copyright © 2017年 LH. All rights reserved.
//

import UIKit

class WeiboMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupChildControllers()
        setupClildButton()
        
    }
    /*
     portrait :竖屏  肖像
     landscape :横屏  风景画
     设置支持方向之后，单钱的控制器及子控制器都会遵守这个方向
     如果播放视频是通过modal展现的
     */
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask{
        return .portrait;
    }
    func conposeStatus()  {
        print("撰写微博")
        let vc = UIViewController()
        let nav = UINavigationController(rootViewController: vc)
        
        vc.view.backgroundColor = UIColor.cz_random()
        
        present(nav, animated: true) {
            
        }
        
    }
    //撰写按钮
    public lazy var composeButton: UIButton = UIButton.cz_imageButton("tabbar_compose_icon_add", backgroundImageName: "tabbar_compose_button")
}
//类似于OC中的分类 在swift中可以用来切分代码块
//可以吧相近功能的函数，放在一个extension中
//便于代码维护
//不能定义属性
extension WeiboMainViewController{
    
    fileprivate func setupClildButton(){
        tabBar.addSubview(composeButton)
        
        //计算按钮的宽度
        let count = CGFloat(childViewControllers.count)
        
//        composeButton.center = tabBar.center
        let w = tabBar.bounds.width / count - 1//容错点  两个按钮之间有一个点会露出去  -1将缩进宽度减小
        //左右缩进  上下缩进
        composeButton.frame = tabBar.bounds.insetBy(dx: 2 * w, dy: 0)
        
        composeButton.addTarget(self, action: #selector(conposeStatus), for: UIControlEvents.touchUpInside)
    }
    
    //设置所有子控制器
    fileprivate func setupChildControllers(){
        let array = [
            ["clsName":"WeiboHomeViewController","title":"首页","imageName":"home"],
            ["clsName":"WeiboMessageViewController","title":"消息","imageName":"message_center"],
            ["clsName":"UIViewController"],
            ["clsName":"WeiboDiscoverViewController","title":"发现","imageName":"discover"],
            
            ["clsName":"WeiboProfileViewController","title":"个人","imageName":"profile"],
        ]
        var arrayM = [UIViewController]()
        
        for dict in array {
            arrayM.append(controller(dict:dict))
        }
        viewControllers = arrayM
    }
    private func controller(dict : [String:String]) ->UIViewController{
       guard let clsName = dict["clsName"],
        let title = dict["title"],
        
        let imageName = dict["imageName"],
        let cls = NSClassFromString(Bundle.main.namespace + "." + clsName) as? UIViewController.Type
        else{
            return UIViewController();
        }
        let vc = cls.init();
        vc.title=title;
        //设置图像
        vc.tabBarItem.image = UIImage(named: "tabbar_" + imageName)
        vc.tabBarItem.selectedImage = UIImage(named: "tabbar_" + imageName + "_selected")?.withRenderingMode(.alwaysOriginal)
        //设置字体
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orange], for: .highlighted)
        //默认12
        vc.tabBarItem.setTitleTextAttributes([NSFontAttributeName:UIFont.systemFont(ofSize: 15)], for:.normal)
        let nav = WeiboMianNavController(rootViewController:vc)
        return nav
        
        

        
    }
}
